package gradle.rabbit.common.config;

import gradle.rabbit.common.constants.RabbitContainerConstant;
import gradle.rabbit.common.constants.RabbitTemplateConstant;
import gradle.rabbit.common.properties.CandidateRabbitProperty;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 备用MQ配置
 *
 * @author 1141193930@qq.com
 */
@Configuration
@ConditionalOnBean(CandidateRabbitProperty.class)
public class CandidateConnectionConfig {

    /**
     * 注入属性配置
     */
    @Resource
    private CandidateRabbitProperty candidateRabbitProperty;


    /**
     * 创建RabbitMQ连接工厂
     *
     * @return {@link ConnectionFactory}
     */
    @Bean
    public ConnectionFactory candidateConnectionFactory() {
        boolean sslEnabled = candidateRabbitProperty.isSslEnabled();
        boolean sslVerifyHostname = candidateRabbitProperty.isSslVerifyHostname();
        boolean sslValidateServerCertificate = candidateRabbitProperty.isSslValidateServerCertificate();
        // 获取配置Bean
        RabbitConnectionFactoryBean factoryBean = BaseRabbitConnectionFactory.getConnectionFactoryBean(sslEnabled, sslVerifyHostname, sslValidateServerCertificate);
        // 获取配置工厂
        CachingConnectionFactory connectionFactory = BaseRabbitConnectionFactory.getCacheConnectionFactory(factoryBean);
        connectionFactory.setAddresses(candidateRabbitProperty.getAddresses());
        connectionFactory.setUsername(candidateRabbitProperty.getUsername());
        connectionFactory.setPassword(candidateRabbitProperty.getPassword());
        connectionFactory.setVirtualHost(candidateRabbitProperty.getVirtualHost());
        connectionFactory.setConnectionTimeout(candidateRabbitProperty.getConnectionTimeout());
        return connectionFactory;
    }


    /**
     * 注册RabbitMQ消息发送模版
     *
     * @return {@link RabbitTemplate}
     */
    @Bean(name = RabbitTemplateConstant.CANDIDATE_RABBIT_TEMPLATE)
    public RabbitTemplate candidateRabbitTemplate() {
        boolean retryEnabled = candidateRabbitProperty.isTemplateRetryEnabled();
        return BaseRabbitConnectionFactory.getRabbitTemplate(retryEnabled, candidateConnectionFactory());
    }


    /**
     * 注册RabbitMQ消费者容器工厂
     *
     * @return {@link SimpleRabbitListenerContainerFactory}
     */
    @Bean(name = RabbitContainerConstant.CANDIDATE_CONTAINER)
    public SimpleRabbitListenerContainerFactory candidateListenerContainerFactory() {
        return BaseRabbitConnectionFactory.getRabbitListenerContainer(candidateConnectionFactory());
    }


    /**
     * 注册RabbitAdmin
     *
     * @return {@link RabbitAdmin}
     */
    @Bean
    public RabbitAdmin candidateRabbitAdmin() {
        return BaseRabbitConnectionFactory.getRabbitAdmin(candidateConnectionFactory());
    }
}
